home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / lib / partman / definitions.sh < prev    next >
Encoding:
Text File  |  2007-03-27  |  28.2 KB  |  1,162 lines

  1.  
  2. . /usr/share/debconf/confmodule
  3.  
  4. NBSP=' '
  5. TAB='    '
  6. NL='
  7. '
  8. ORIGINAL_IFS="${ORIGINAL_IFS:-$IFS}"; export ORIGINAL_IFS
  9.  
  10. restore_ifs () {
  11.     IFS="$ORIGINAL_IFS"
  12. }
  13.  
  14. dirname () {
  15.     local x
  16.     x="${1%/}"
  17.     echo "${x%/*}"
  18. }
  19.  
  20. basename () {
  21.     local x
  22.     x="${1%$2}"
  23.     x="${x%/}"
  24.     echo "${x##*/}"
  25. }
  26.  
  27. can_escape () {
  28.     type debconf-escape >/dev/null 2>&1 || return 1
  29.     db_capb backup
  30.     for cap in $RET; do
  31.         case $cap in
  32.             escape)    return 0 ;;
  33.         esac
  34.     done
  35.     return 1
  36. }
  37.  
  38. maybe_escape () {
  39.     local code saveret
  40.     text="$1"
  41.     shift
  42.     if can_escape; then
  43.         db_capb backup escape
  44.         code=0
  45.         "$@" "$(printf '%s' "$text" | debconf-escape -e)" || code=$?
  46.         saveret="$RET"
  47.         db_capb backup
  48.         RET="$saveret"
  49.         return $code
  50.     else
  51.         "$@" "$text"
  52.     fi
  53. }
  54.  
  55. debconf_select () {
  56.     local IFS priority template choices default_choice default x u newchoices code
  57.     priority="$1"
  58.     template="$2"
  59.     choices="$3"
  60.     default_choice="$4"
  61.     default=''
  62.     # Debconf ignores spaces so we have to remove them from $choices
  63.     newchoices=''
  64.     case $PARTMAN_SNOOP in
  65.         ?*)
  66.             > /var/lib/partman/snoop
  67.             ;;
  68.     esac
  69.     IFS="$NL"
  70.     for x in $choices; do
  71.         local key option
  72.         restore_ifs
  73.         key=$(echo ${x%$TAB*})
  74.         # work around bug #243373
  75.         if [ "$TERM" = xterm -o "$TERM" = bterm ]; then
  76.             debconf_select_lead="$NBSP"
  77.         else
  78.             debconf_select_lead="> "
  79.         fi
  80.         option=$(echo "${x#*$TAB}" | sed "s/ *\$//g; s/^ /$debconf_select_lead/g")
  81.         newchoices="${newchoices:+${newchoices}${NL}}${key}${TAB}${option}"
  82.         if [ "$key" = "$default_choice" ]; then
  83.             default="$option"
  84.         fi
  85.         case $PARTMAN_SNOOP in
  86.             ?*)
  87.                 echo "$key$TAB$option" >> /var/lib/partman/snoop
  88.                 ;;
  89.         esac
  90.     done
  91.     choices="$newchoices"
  92.     u=''
  93.     IFS="$NL"
  94.     # escape the commas and leading whitespace but keep them unescaped
  95.     # in $choices
  96.         for x in $choices; do
  97.                 u="$u, `echo ${x#*$TAB} | sed 's/,/\\\\,/g; s/^ /\\\\ /'`"
  98.         done
  99.         u=${u#, }
  100.     restore_ifs
  101.     # TODO: This can be preseeded without having to use translated
  102.     # values (which are often inappropriate for preseeding across many
  103.     # machines due to including e.g. disk capacities) but it's nasty;
  104.     # you have to use runes like
  105.     # "20some_device__________/var/lib/partman/devices/=dev=sda". We
  106.     # could do with an abbreviated syntax.
  107.     if [ -n "$default" ]; then
  108.             db_set $template "$default"
  109.     fi
  110.     db_subst $template CHOICES "$u"
  111.     code=0
  112.     db_input $priority $template || code=1
  113.     db_go || return 255
  114.     db_get $template
  115.     IFS="$NL"
  116.     for x in $choices; do
  117.         if [ "$RET" = "${x#*$TAB}" ]; then
  118.             RET="${x%$TAB*}"
  119.             break
  120.         fi
  121.     done
  122.     restore_ifs
  123.     return $code
  124. }
  125.  
  126. menudir_default_choice () {
  127.     printf "%s__________%s\n" "$(basename $1/??$2)" "$3" > $1/default_choice
  128. }
  129.  
  130. ask_user () {
  131.     local IFS dir template priority default choices plugin name option
  132.     dir="$1"; shift
  133.     template=$(cat $dir/question)
  134.     priority=$(cat $dir/priority)
  135.     if [ -f $dir/default_choice ]; then
  136.     default=$(cat $dir/default_choice)
  137.     else
  138.     default=""
  139.     fi
  140.     choices=$(
  141.     for plugin in $dir/*; do
  142.         [ -d $plugin ] || continue
  143.         name=$(basename $plugin)
  144.         IFS="$NL"
  145.         for option in $($plugin/choices "$@"); do
  146.         printf "%s__________%s\n" $name "$option"
  147.         done
  148.         restore_ifs
  149.     done
  150.     )
  151.     code=0
  152.     debconf_select $priority $template "$choices" "$default" || code=$?
  153.     if [ $code -ge 100 ]; then return 255; fi
  154.     echo "$RET" >$dir/default_choice
  155.     $dir/${RET%__________*}/do_option ${RET#*__________} "$@" || return $?
  156.     return 0
  157. }
  158.  
  159. partition_tree_choices () {
  160.     local IFS
  161.     local whitespace_hack=""
  162.     for dev in $DEVICES/*; do
  163.     [ -d $dev ] || continue
  164.     printf "%s//\t%s\n" $dev "$(device_name $dev)" # GETTEXT?
  165.     cd $dev
  166.  
  167.     open_dialog PARTITIONS
  168.     partitions="$(read_paragraph)"
  169.     close_dialog
  170.     
  171.     IFS="$TAB"
  172.     echo "$partitions" |
  173.     while { read num id size type fs path name; [ "$id" ]; }; do
  174.         part=${dev}/$id
  175.         [ -f $part/view ] || continue
  176.         printf "%s//%s\t     %s\n" "$dev" "$id" $(cat $part/view)
  177.     done
  178.     restore_ifs
  179.     done | while read line; do
  180.         # A hack to make sure each line in the table is unique and
  181.     # selectable by debconf -- pad lines with varying amounts of
  182.     # whitespace.
  183.         whitespace_hack="$NBSP$whitespace_hack"
  184.     echo "$line$whitespace_hack"
  185.     done
  186. }
  187.  
  188. longint_le () {
  189.     local x y
  190.     # remove the leading 0
  191.     x=$(expr "$1" : '0*\(.*\)')
  192.     y=$(expr "$2" : '0*\(.*\)')
  193.     if [ ${#x} -lt ${#y} ]; then
  194.         return 0
  195.     elif [ ${#x} -gt ${#y} ]; then
  196.         return 1
  197.     elif [ "$x" = "$y" ]; then
  198.         return 0
  199.     elif [ "$x" '<' "$y" ]; then
  200.         return 0
  201.     else
  202.         return 1
  203.     fi
  204. }
  205.  
  206. longint2human () {
  207.     local longint suffix bytes int frac deci
  208.     # fallback value for $deci:
  209.     deci="${deci:-.}"
  210.     case ${#1} in
  211.         1|2|3)
  212.         suffix=B
  213.         longint=${1}00
  214.         ;;
  215.         4|5|6)
  216.         suffix=kB
  217.         longint=${1%?}
  218.         ;;
  219.         7|8|9)
  220.         suffix=MB
  221.         longint=${1%????}
  222.         ;;
  223.         10|11|12)
  224.         suffix=GB
  225.         longint=${1%???????}
  226.         ;;
  227.         *)
  228.         suffix=TB
  229.         longint=${1%??????????}
  230.         ;;
  231.     esac
  232.     longint=$(($longint + 5))
  233.     longint=${longint%?}
  234.     int=${longint%?}
  235.     frac=${longint#$int}
  236.     printf "%i%s%i %s\n" $int $deci $frac $suffix
  237. }
  238.  
  239. human2longint () {
  240.     local human suffix int frac longint
  241.     set -- $*; human="$1$2$3$4$5" # without the spaces
  242.     human=${human%b} #remove last b
  243.     human=${human%B} #remove last B
  244.     suffix=${human#${human%?}} # the last symbol of $human
  245.     case $suffix in
  246.     k|K|m|M|g|G|t|T)
  247.         human=${human%$suffix}
  248.         ;;
  249.     *)
  250.         suffix=''
  251.         ;;
  252.     esac
  253.     int="${human%[.,]*}"
  254.     [ "$int" ] || int=0
  255.     frac=${human#$int}
  256.     frac="${frac#[.,]}0000" # to be sure there are at least 4 digits
  257.     frac=${frac%${frac#????}} # only the first 4 digits of $frac
  258.     longint=$(($int * 10000 + $frac))
  259.     case $suffix in
  260.     k|K)
  261.         longint=${longint%?}
  262.         ;;
  263.     m|M)
  264.         longint=${longint}00
  265.         ;;
  266.     g|G)
  267.         longint=${longint}00000
  268.         ;;
  269.     t|T)
  270.         longint=${longint}00000000
  271.         ;;
  272.     *) # no suffix:
  273.         # bytes
  274.         #longint=${longint%????}
  275.         #[ "$longint" ] || longint=0
  276.         # megabytes
  277.         longint=${longint}00
  278.         ;;
  279.     esac
  280.     echo $longint
  281. }
  282.  
  283. valid_human () {
  284.     local IFS patterns
  285.     patterns='[0-9][0-9]* *$
  286. [0-9][0-9]* *[bB] *$
  287. [0-9][0-9]* *[kKmMgGtT] *$
  288. [0-9][0-9]* *[kKmMgGtT][bB] *$
  289. [0-9]*[.,][0-9]* *$
  290. [0-9]*[.,][0-9]* *[bB] *$
  291. [0-9]*[.,][0-9]* *[kKmMgGtT] *$
  292. [0-9]*[.,][0-9]* *[kKmMgGtT][bB] *$'
  293.     IFS="$NL"
  294.     for regex in $patterns; do
  295.         if expr "$1" : "$regex" >/dev/null; then return 0; fi
  296.     done
  297.     return 1
  298. }
  299.  
  300. stop_parted_server () {
  301.     open_infifo
  302.     write_line "QUIT"
  303.     close_infifo
  304. }
  305.  
  306. # Must call stop_parted_server before calling this.
  307. restart_partman () {
  308.     initcount=`ls /lib/partman/init.d/* | wc -l`
  309.     db_progress START 0 $initcount partman/progress/init/title
  310.     for s in /lib/partman/init.d/*; do
  311.     if [ -x $s ]; then
  312.         base=$(basename $s | sed 's/[0-9]*//')
  313.         if ! db_progress INFO partman/progress/init/$base; then
  314.         db_progress INFO partman/progress/init/fallback
  315.         fi
  316.         if ! $s; then
  317.         db_progress STOP
  318.         exit 255
  319.         fi
  320.     fi
  321.     db_progress STEP 1
  322.     done
  323.     db_progress STOP
  324. }
  325.  
  326. update_partition () {
  327.     local u
  328.     cd $1
  329.     open_dialog PARTITION_INFO $2
  330.     read_line part
  331.     close_dialog
  332.     [ "$part" ] || return 0
  333.     for u in /lib/partman/update.d/*; do
  334.     [ -x "$u" ] || continue
  335.     $u $1 $part
  336.     done
  337. }
  338.         
  339. DEVICES=/var/lib/partman/devices
  340.  
  341. # 0, 1 and 2 are standard input, output and error.
  342. # 3, 4 and 5 are used by cdebconf
  343. # 6=infifo
  344. # 7=outfifo
  345.  
  346. open_infifo() {
  347.     exec 6>/var/lib/partman/infifo
  348. }
  349.  
  350. close_infifo() {
  351.     exec 6>&-
  352. }
  353.  
  354. open_outfifo () {
  355.     exec 7</var/lib/partman/outfifo
  356. }
  357.  
  358. close_outfifo () {
  359.     exec 7<&-
  360. }
  361.  
  362. write_line () {
  363.     log IN: "$@"
  364.     echo "$@" >&6
  365. }
  366.  
  367. read_line () {
  368.     read "$@" <&7
  369. }
  370.  
  371. synchronise_with_server () {
  372.     exec 6>/var/lib/partman/stopfifo
  373.     exec 6>&-
  374. }
  375.  
  376. read_paragraph () {
  377.     local line
  378.     while { read_line line; [ "$line" ]; }; do
  379.     log "paragraph: $line"
  380.     echo "$line"
  381.     done
  382. }
  383.  
  384. read_list () {
  385.     local item list
  386.     list=''
  387.     while { read_line item; [ "$item" ]; }; do
  388.     log "option: $item"
  389.     if [ "$list" ]; then
  390.         list="$list, $item"
  391.     else
  392.         list="$item"
  393.     fi
  394.     done
  395.     echo "$list"
  396. }
  397.  
  398. name_progress_bar () {
  399.     echo $1 >/var/lib/partman/progress_info
  400. }
  401.  
  402. error_handler () {
  403.     local exception_type info state frac type priority message options skipped
  404.     while { read_line exception_type; [ "$exception_type" != OK ]; }; do
  405.     log error_handler: exception with type $exception_type
  406.     case "$exception_type" in
  407.         Timer)
  408.         if [ -f /var/lib/partman/progress_info ]; then
  409.             info=$(cat /var/lib/partman/progress_info)
  410.         else
  411.             info=partman/processing
  412.         fi
  413.         db_progress START 0 1000 partman/text/please_wait
  414.         db_progress INFO $info
  415.         while { read_line frac state; [ "$frac" != ready ]; }; do
  416.             if [ "$state" ]; then
  417.             db_subst $info STATE "$state" 
  418.             db_progress INFO $info
  419.             fi
  420.             db_progress SET $frac
  421.         done
  422.         db_progress STOP
  423.         continue
  424.         ;;
  425.         Information)
  426.         type='Information'
  427.         priority=medium
  428.         ;;
  429.         Warning)
  430.         type='Warning!'
  431.         priority=high
  432.         ;;
  433.         Error)
  434.         type='ERROR!!!'
  435.         priority=critical
  436.         ;;
  437.         Fatal)
  438.         type='FATAL ERROR!!!'
  439.         priority=critical
  440.         ;;
  441.         Bug)
  442.         type='A bug has been discovered!!!'
  443.         priority=critical
  444.         ;;
  445.         No?Implementation)
  446.         type='Not yet implemented!'
  447.         priority=critical
  448.         ;;
  449.         *)
  450.         type="??? $exception_type ???"
  451.         priority=critical
  452.         ;;
  453.     esac
  454.     log error_handler: reading message
  455.     message=$(read_paragraph)
  456.     log error_handler: reading options
  457.     options=$(read_list)
  458.     db_subst partman/exception_handler TYPE "$type"
  459.     maybe_escape "$message" db_subst partman/exception_handler DESCRIPTION
  460.     db_subst partman/exception_handler CHOICES "$options"
  461.     if
  462.         expr "$options" : '.*,.*' >/dev/null \
  463.         && db_input $priority partman/exception_handler
  464.     then
  465.         if db_go; then
  466.         db_get partman/exception_handler
  467.         write_line "$RET"
  468.         else
  469.         write_line "unhandled"
  470.         fi
  471.     else
  472.         db_subst partman/exception_handler_note TYPE "$type"
  473.         maybe_escape "$message" db_subst partman/exception_handler_note DESCRIPTION
  474.         db_input $priority partman/exception_handler_note || true
  475.         db_go || true
  476.         write_line "unhandled"
  477.     fi
  478.     done
  479.     rm -f /var/lib/partman/progress_info
  480. }
  481.  
  482. open_dialog () {
  483.     command="$1"
  484.     shift
  485.     open_infifo
  486.     write_line "$command" "${PWD##*/}" "$@"
  487.     open_outfifo
  488.     error_handler
  489. }
  490.  
  491. close_dialog () {
  492.     close_outfifo
  493.     close_infifo
  494.     exec 6>/var/lib/partman/stopfifo
  495.     exec 6>&-
  496.     exec 7>/var/lib/partman/outfifo
  497.     exec 7>&-
  498.     exec 6>/var/lib/partman/stopfifo
  499.     exec 6>&-
  500.     exec 6</var/lib/partman/infifo
  501.     cat <&6 >/dev/null
  502.     exec 6<&-
  503.     exec 6>/var/lib/partman/stopfifo
  504.     exec 6>&-
  505. }
  506.  
  507. log () {
  508.     local program
  509.     echo $0: "$@" >>/var/log/partman
  510. }
  511.  
  512. ####################################################################
  513. # The functions below are not yet documented
  514. ####################################################################
  515.  
  516. # TODO: this should not be global
  517. humandev () {
  518.     local host bus target part lun idenum targtype scsinum linux
  519.     case "$1" in
  520.     /dev/ide/host*/bus[01]/target[01]/lun0/disc)
  521.         host=`echo $1 | sed 's,/dev/ide/host\(.*\)/bus.*/target[01]/lun0/disc,\1,'`
  522.         bus=`echo $1 | sed 's,/dev/ide/host.*/bus\(.*\)/target[01]/lun0/disc,\1,'`
  523.         target=`echo $1 | sed 's,/dev/ide/host.*/bus.*/target\([01]\)/lun0/disc,\1,'`
  524.         idenum=$((2 * $host + $bus + 1))
  525.         linux=$(mapdevfs $1)
  526.         linux=${linux#/dev/}
  527.         if [ "$target" = 0 ]; then
  528.         db_metaget partman/text/ide_master_disk description
  529.         printf "$RET" ${idenum} ${linux}
  530.         else
  531.         db_metaget partman/text/ide_slave_disk description
  532.         printf "$RET" ${idenum} ${linux}
  533.         fi
  534.         ;;
  535.     # Some drivers advertise the disk as "part", workaround for #404950
  536.     /dev/ide/host*/bus[01]/target[01]/lun0/part)
  537.         host=`echo $1 | sed 's,/dev/ide/host\(.*\)/bus.*/target[01]/lun0/part,\1,'`
  538.         bus=`echo $1 | sed 's,/dev/ide/host.*/bus\(.*\)/target[01]/lun0/part,\1,'`
  539.         target=`echo $1 | sed 's,/dev/ide/host.*/bus.*/target\([01]\)/lun0/part,\1,'`
  540.         idenum=$((2 * $host + $bus + 1))
  541.         linux=$(mapdevfs $1)
  542.         linux=${linux#/dev/}
  543.         if [ "$target" = 0 ]; then
  544.         db_metaget partman/text/ide_master_disk description
  545.         printf "$RET" ${idenum} ${linux}
  546.         else
  547.         db_metaget partman/text/ide_slave_disk description
  548.         printf "$RET" ${idenum} ${linux}
  549.         fi
  550.         ;;
  551.     /dev/ide/host*/bus[01]/target[01]/lun0/part*)
  552.         host=`echo $1 | sed 's,/dev/ide/host\(.*\)/bus.*/target[01]/lun0/part.*,\1,'`
  553.         bus=`echo $1 | sed 's,/dev/ide/host.*/bus\(.*\)/target[01]/lun0/part.*,\1,'`
  554.         target=`echo $1 | sed 's,/dev/ide/host.*/bus.*/target\([01]\)/lun0/part.*,\1,'`
  555.         part=`echo $1 | sed 's,/dev/ide/host.*/bus.*/target[01]/lun0/part\(.*\),\1,'`
  556.         idenum=$((2 * $host + $bus + 1))
  557.         linux=$(mapdevfs $1)
  558.         linux=${linux#/dev/}
  559.         if [ "$target" = 0 ]; then
  560.         db_metaget partman/text/ide_master_partition description
  561.         printf "$RET" ${idenum} "$part" "${linux}"
  562.         else
  563.         db_metaget partman/text/ide_slave_partition description
  564.         printf "$RET" ${idenum} "$part" "${linux}"
  565.         fi
  566.         ;;
  567.     /dev/hd[a-z])
  568.         drive=$(printf '%d' "'$(echo $1 | sed 's,^/dev/hd\([a-z]\).*,\1,')")
  569.         drive=$(($drive - 97))
  570.         linux=${1#/dev/}
  571.         if [ "$(($drive % 2))" = 0 ]; then
  572.         db_metaget partman/text/ide_master_disk description
  573.         else
  574.         db_metaget partman/text/ide_slave_disk description
  575.         fi
  576.         printf "$RET" "$(($drive / 2 + 1))" "$linux"
  577.         ;;
  578.     /dev/hd[a-z][0-9]*)
  579.         drive=$(printf '%d' "'$(echo $1 | sed 's,^/dev/hd\([a-z]\).*,\1,')")
  580.         drive=$(($drive - 97))
  581.         part=$(echo $1 | sed 's,^/dev/hd[a-z]\([0-9][0-9]*\).*,\1,')
  582.         linux=${1#/dev/}
  583.         if [ "$(($drive % 2))" = 0 ]; then
  584.         db_metaget partman/text/ide_master_partition description
  585.         else
  586.         db_metaget partman/text/ide_slave_partition description
  587.         fi
  588.         printf "$RET" "$(($drive / 2 + 1))" "$part" "$linux"
  589.         ;;
  590.     /dev/scsi/host*/bus*/target*/lun*/disc)
  591.         host=`echo $1 | sed 's,/dev/scsi/host\(.*\)/bus.*/target.*/lun.*/disc,\1,'`
  592.         bus=`echo $1 | sed 's,/dev/scsi/host.*/bus\(.*\)/target.*/lun.*/disc,\1,'`
  593.         target=`echo $1 | sed 's,/dev/scsi/host.*/bus.*/target\(.*\)/lun.*/disc,\1,'`
  594.         lun=`echo $1 | sed 's,/dev/scsi/host.*/bus.*/target.*/lun\(.*\)/disc,\1,'`
  595.         scsinum=$(($host + 1))
  596.         linux=$(mapdevfs $1)
  597.         linux=${linux#/dev/}
  598.         db_metaget partman/text/scsi_disk description
  599.         printf "$RET" ${scsinum} ${bus} ${target} ${lun} ${linux}
  600.         ;;
  601.     /dev/scsi/host*/bus*/target*/lun*/part*)
  602.         host=`echo $1 | sed 's,/dev/scsi/host\(.*\)/bus.*/target.*/lun.*/part.*,\1,'`
  603.         bus=`echo $1 | sed 's,/dev/scsi/host.*/bus\(.*\)/target.*/lun.*/part.*,\1,'`
  604.         target=`echo $1 | sed 's,/dev/scsi/host.*/bus.*/target\(.*\)/lun.*/part.*,\1,'`
  605.         lun=`echo $1 | sed 's,/dev/scsi/host.*/bus.*/target.*/lun\(.*\)/part.*,\1,'`
  606.         part=`echo $1 | sed 's,/dev/scsi/host.*/bus.*/target.*/lun.*/part\(.*\),\1,'`
  607.         scsinum=$(($host + 1))
  608.         linux=$(mapdevfs $1)
  609.         linux=${linux#/dev/}
  610.         db_metaget partman/text/scsi_partition description
  611.         printf "$RET" ${scsinum} ${bus} ${target} ${lun} ${part} ${linux}
  612.         ;;
  613.     /dev/sd[a-z]|/dev/sd[a-z][a-z])
  614.         disk="${1#/dev/}"
  615.         if [ -h "/sys/block/$disk/device" ]; then
  616.         bus_id="$(basename "$(readlink "/sys/block/$disk/device")")"
  617.         host="${bus_id%%:*}"
  618.         bus_id="${bus_id#*:}"
  619.         bus="${bus_id%%:*}"
  620.         bus_id="${bus_id#*:}"
  621.         target="${bus_id%%:*}"
  622.         lun="${bus_id#*:}"
  623.         scsinum="$(($host + 1))"
  624.         db_metaget partman/text/scsi_disk description
  625.         printf "$RET" "$scsinum" "$bus" "$target" "$lun" "$disk"
  626.         else
  627.         # Can't figure out host/bus/target/lun without sysfs, but
  628.         # never mind; if we don't have sysfs then we're probably on
  629.         # 2.4 and devfs anyway.
  630.         echo "$1"
  631.         fi
  632.         ;;
  633.     /dev/sd[a-z][0-9]*|/dev/sd[a-z][a-z][0-9]*)
  634.         part="${1#/dev/}"
  635.         disk="${part%%[0-9]*}"
  636.         part="${part#$disk}"
  637.         if [ -h "/sys/block/$disk/device" ]; then
  638.         bus_id="$(basename "$(readlink "/sys/block/$disk/device")")"
  639.         host="${bus_id%%:*}"
  640.         bus_id="${bus_id#*:}"
  641.         bus="${bus_id%%:*}"
  642.         bus_id="${bus_id#*:}"
  643.         target="${bus_id%%:*}"
  644.         lun="${bus_id#*:}"
  645.         scsinum="$(($host + 1))"
  646.         db_metaget partman/text/scsi_partition description
  647.         printf "$RET" "$scsinum" "$bus" "$target" "$lun" "$part" "$disk"
  648.         else
  649.         # Can't figure out host/bus/target/lun without sysfs, but
  650.         # never mind; if we don't have sysfs then we're probably on
  651.         # 2.4 and devfs anyway.
  652.         echo "$1"
  653.         fi
  654.         ;;
  655.     /dev/cciss/host*|/dev/cciss/disc*)
  656.         # /dev/cciss/hostN/targetM/disc is 2.6 devfs form
  657.         # /dev/cciss/discM/disk seems to be 2.4 devfs form
  658.         line=`echo $1 | sed 's,/dev/cciss/\([a-z]*\)\([0-9]*\)/\(.*\),\1 \2 \3,'`
  659.         controller=`echo "$line" | cut -d" " -f2`
  660.         host=`echo "$line" | cut -d" " -f1`
  661.         line=`echo "$line" | cut -d" " -f3`
  662.         if [ "$host" = host ] ; then
  663.            line=`echo "$line" | sed 's,target\([0-9]*\)/\([a-z]*\)\(.*\),\1 \2 \3,'`
  664.            lun=`echo  "$line" | cut -d" " -f1`
  665.            disc=`echo "$line" | cut -d" " -f2`
  666.            part=`echo "$line" | cut -d" " -f3`
  667.         else
  668.            line=`echo "$line" | sed 's,disc\([0-9]*\)/\([a-z]*\)\(.*\),\1 \2 \3,'`
  669.            lun=`echo  "$line" | cut -d" " -f1`
  670.            if [ "$lun" > 15 ] ; then
  671.               controller=$(($lun / 16))
  672.           lun=$(($lun % 16))
  673.            else
  674.           controller=0
  675.            fi
  676.            disc=`echo "$line" | cut -d" " -f2`
  677.            part=`echo "$line" | cut -d" " -f3`
  678.         fi
  679.         linux=$(mapdevfs $1)
  680.         linux=${linux#/dev/}
  681.         if [ "$disc" = disc ] ; then
  682.            db_metaget partman/text/scsi_disk description
  683.            printf "$RET" ".CCISS" "-" ${controller} ${lun} ${linux}
  684.         else
  685.            db_metaget partman/text/scsi_partition description
  686.            printf "$RET" ".CCISS" "-" ${controller} ${lun} ${part} ${linux}
  687.         fi
  688.         ;;
  689.     /dev/cciss/c*d*)
  690.         # It would be a lot easier to parse the /sys/block/*/device
  691.         # symlink. Unfortunately, unlike other block devices, this
  692.         # doesn't seem to exist in this case, so we just have to live
  693.         # with parsing the device name (note: added in upstream 2.6.18).
  694.         controller="$(echo "$1" | sed 's,/dev/cciss/c\([0-9]*\).*,\1,')"
  695.         lun="$(echo "$1" | sed 's,/dev/cciss/c[0-9]*d\([0-9]*\).*,\1,')"
  696.         case $1 in
  697.         /dev/cciss/c*d*p*)
  698.             # partition
  699.             part="$(echo "$1" | sed 's,/dev/cciss/c[0-9]*d[0-9]*p\([0-9]*\).*,\1,')"
  700.             ;;
  701.         *)
  702.             part=
  703.             ;;
  704.         esac
  705.         linux="$(mapdevfs "$1")"
  706.         linux="${linux#/dev/}"
  707.         if [ -z "$part" ]; then
  708.         db_metaget partman/text/scsi_disk description
  709.         printf "$RET" ".CCISS" "-" "$controller" "$lun" "$linux"
  710.         else
  711.         db_metaget partman/text/scsi_partition description
  712.         printf "$RET" ".CCISS" "-" "$controller" "$lun" "$part" "$linux"
  713.         fi
  714.         ;;
  715.     /dev/md*|/dev/md/*)
  716.         device=`echo "$1" | sed -e "s/.*md\/\?\(.*\)/\1/"`
  717.         type=`grep "^md${device}[ :]" /proc/mdstat | sed -e "s/^.* : active raid\([[:alnum:]]\).*/\1/"`
  718.         db_metaget partman/text/raid_device description
  719.         printf "$RET" ${type} ${device}
  720.         ;;
  721.     /dev/mapper/*)
  722.         # First of all, check if this is a dm-crypt device
  723.         type=""
  724.         if [ -x /sbin/dmsetup ]; then
  725.             type=$(/sbin/dmsetup table "$1" | head -n 1 | cut -d " " -f3)
  726.         fi
  727.  
  728.         if [ "$type" = crypt ]; then
  729.             mapping=${1#/dev/mapper/}
  730.             db_metaget partman/text/dmcrypt_volume description
  731.             printf "$RET" $mapping
  732.         else
  733.             # LVM2 devices are found as /dev/mapper/<vg>-<lv>.  If the vg
  734.             # or lv contains a dash, the dash is replaced by two dashes.
  735.             # In order to decode this into vg and lv, first find the
  736.             # occurance of one single dash to split the string into vg and
  737.             # lv, and then replace two dashes next to each other with one.
  738.             vglv=${1#/dev/mapper/}
  739.             vglv=`echo "$vglv" | sed 's/\([^-]\)-\([^-]\)/\1 \2/; s/--/-/g'`
  740.             vg=`echo "$vglv" | cut -d" " -f1`
  741.             lv=`echo "$vglv" | cut -d" " -f2`
  742.             db_metaget partman/text/lvm_lv description
  743.             printf "$RET" $vg $lv
  744.         fi
  745.         ;;
  746.     /dev/loop/*|/dev/loop*)
  747.         n=${1#/dev/loop}
  748.         n=${n#/}
  749.         db_metaget partman/text/loopback description
  750.         printf "$RET" $n
  751.         ;;
  752.     # DASD partition, classic
  753.     /dev/dasd*[0-9]*)
  754.         part="${1#/dev/}"
  755.         disk="${part%%[0-9]*}"
  756.         part="${part#$disk}"
  757.         humandev_dasd_partition /sys/block/$disk/$(readlink /sys/block/$disk/device) $part
  758.         ;;
  759.     # DASD disk, classic
  760.     /dev/dasd*)
  761.         disk="${1#/dev/}"
  762.         humandev_dasd_disk /sys/block/$disk/$(readlink /sys/block/$disk/device)
  763.         ;;
  764.     *)
  765.         # Check if it's an LVM1 device
  766.         vg=`echo "$1" | sed -e 's,/dev/\([^/]\+\).*,\1,'`
  767.         lv=`echo "$1" | sed -e 's,/dev/[^/]\+/,,'`
  768.         if [ -e "/proc/lvm/VGs/$vg/LVs/$lv" ] ; then
  769.         db_metaget partman/text/lvm_lv description
  770.         printf "$RET" $vg $lv
  771.         else
  772.         echo "$1"
  773.         fi
  774.         ;;
  775.     esac
  776. }
  777.  
  778. humandev_dasd_disk () {
  779.     dev=${1##*/}
  780.     discipline=$(cat $1/discipline)
  781.     db_metaget partman/text/dasd_disk description
  782.     printf "$RET" "$dev" "$discipline"
  783. }
  784.  
  785. humandev_dasd_partition () {
  786.     dev=${1##*/}
  787.     discipline=$(cat $1/discipline)
  788.     db_metaget partman/text/dasd_partition description
  789.     printf "$RET" "$dev" "$discipline" "$part"
  790. }
  791.  
  792. device_name () {
  793.     cd $1
  794.     printf "%s - %s %s" "$(humandev $(cat device))" "$(longint2human $(cat size))" "$(cat model)"
  795. }
  796.  
  797. enable_swap () {
  798.     local swaps dev num id size type fs path name method
  799.     local startdir="$(pwd)"
  800.     # do swapon only when we will be able to swapoff afterwards
  801.     [ -f /proc/swaps ] || return 0
  802.     swaps=''
  803.     for dev in $DEVICES/*; do
  804.     [ -d $dev ] || continue
  805.     cd $dev
  806.     open_dialog PARTITIONS
  807.     while { read_line num id size type fs path name; [ "$id" ]; }; do
  808.         [ $fs != free ] || continue
  809.         [ -f "$id/method" ] || continue
  810.         method=$(cat $id/method)
  811.         if [ "$method" = swap ]; then
  812.         swaps="$swaps $path"
  813.         fi
  814.     done
  815.     close_dialog
  816.     done
  817.     for path in $swaps; do
  818.     if ! grep -q "$path" /proc/swaps; then
  819.         swapon $path 2>/dev/null || true
  820.     fi
  821.     done
  822.     cd "$startdir"
  823. }
  824.  
  825. disable_swap () {
  826.     [ -f /proc/swaps ] || return 0
  827.     grep '^/dev' /proc/swaps \
  828.     | while read path x; do
  829.           swapoff $path
  830.           done
  831. }
  832.  
  833. default_disk_label () {
  834.     if [ -x /bin/archdetect ]; then
  835.     archdetect=$(archdetect)
  836.     else
  837.     archdetect=unknown/generic
  838.     fi
  839.     arch=${archdetect%/*}
  840.     sub=${archdetect#*/}
  841.     case "$arch" in
  842.     alpha)
  843.         # Load srm_env.o if we can; this should fail on ARC-based systems.
  844.         (modprobe srm_env || true) 2> /dev/null
  845.         if [ -f /proc/srm_environment/named_variables/booted_dev ]; then
  846.                 # Have SRM, so need BSD disklabels
  847.         echo bsd
  848.         else
  849.         echo msdos
  850.         fi;;        
  851.     arm|armel)
  852.         case "$sub" in
  853.         iop32x)
  854.             echo msdos;;
  855.         iop33x)
  856.             echo msdos;;
  857.         ixp4xx)
  858.             echo msdos;;
  859.         riscstation)
  860.             echo msdos;;
  861.         netwinder)
  862.             echo msdos;;
  863.         ads)
  864.             echo msdos;;
  865.         versatile)
  866.             echo msdos;;
  867.         *)
  868.             echo UNKNOWN;;
  869.         esac;;
  870.     armeb)
  871.         case "$sub" in
  872.         ixp4xx)
  873.             echo msdos;;
  874.         *)
  875.             echo UNKNOWN;;
  876.         esac;;
  877.     amd64)
  878.         case "$sub" in
  879.         mac)
  880.             echo gpt;;
  881.         *)
  882.             echo msdos;;
  883.         esac;;
  884.     hppa)
  885.         echo msdos;;
  886.     ia64)
  887.         echo gpt;;
  888.     i386)
  889.         case "$sub" in
  890.         mac)
  891.             echo gpt;;
  892.         *)
  893.             echo msdos;;
  894.         esac;;
  895.     m68k)
  896.         case "$sub" in
  897.         amiga)
  898.             echo amiga;;
  899.         atari)
  900.             echo UNSUPPORTED;; # atari is unsupported by parted
  901.         mac)
  902.             echo mac;;
  903.         *vme*)
  904.             echo msdos;;
  905.         q40)
  906.             echo UNSUPPORTED;; # (same as atari)
  907.         sun*)
  908.                 echo sun;;
  909.         *)
  910.             echo UNKNOWN;;
  911.         esac;;
  912.     mips)
  913.         case "$sub" in
  914.         # Indy
  915.         r4k-ip22 | r5k-ip22 | r8k-ip26 | r10k-ip28)
  916.             echo dvh;;
  917.         # Origin
  918.         r10k-ip27 | r12k-ip27)
  919.             echo dvh;;
  920.         # O2
  921.         r5k-ip32 | r10k-ip32 | r12k-ip32)
  922.             echo dvh;;
  923.         # Broadcom SB1 evaluation boards
  924.         sb1-bcm91250a | sb1a-bcm91480b)
  925.             echo msdos;;
  926.         qemu-mips32)
  927.             echo msdos;;
  928.         *)
  929.             echo UNKNOWN;;
  930.         esac;;
  931.     mipsel)
  932.         case "$sub" in
  933.         # DECstation
  934.         r3k-kn02)
  935.             echo msdos;;
  936.         r4k-kn04)
  937.             echo msdos;;
  938.         # Broadcom SB1 evaluation boards
  939.         sb1-bcm91250a | sb1a-bcm91480b)
  940.             echo msdos;;
  941.         cobalt)
  942.             echo msdos;;
  943.         bcm947xx)
  944.             echo msdos;;
  945.         qemu-mips32)
  946.             echo msdos;;
  947.         *)
  948.             echo UNKNOWN;;
  949.         esac;;
  950.     powerpc)
  951.         case "$sub" in
  952.         apus)
  953.             echo amiga;;
  954.         amiga)
  955.             echo amiga;;
  956.         chrp)
  957.             echo msdos;;
  958.         chrp_rs6k|chrp_ibm)
  959.             echo msdos;;
  960.         chrp_pegasos)
  961.             echo amiga;;
  962.         prep)
  963.             echo msdos;;
  964.         powermac_newworld)
  965.             echo mac;;
  966.         powermac_oldworld)
  967.             echo mac;;
  968.         ps3)
  969.             echo msdos;;
  970.         *)
  971.             echo UNKNOWN;;
  972.         esac;;
  973.     s390)
  974.         echo msdos;;
  975.     sparc)
  976.         echo sun;;
  977.     *)
  978.         echo UNKNOWN;;
  979.     esac
  980. }
  981.  
  982. # Lock a device or partition against further modifications
  983. partman_lock_unit() {
  984.     local device message dev testdev
  985.     device="$1"
  986.     message="$2"
  987.  
  988.     for dev in $DEVICES/*; do
  989.         [ -d "$dev" ] || continue
  990.         cd $dev
  991.  
  992.         # First check if we should lock a device
  993.         if [ -e "device" ]; then
  994.             testdev=$(mapdevfs $(cat device))
  995.             if [ "$device" = "$testdev" ]; then
  996.                 echo "$message" > locked
  997.                 return 0
  998.             fi
  999.         fi
  1000.  
  1001.         # Second check if we should lock a partition
  1002.         open_dialog PARTITIONS
  1003.         while { read_line num id size type fs path name; [ "$id" ]; }; do
  1004.             testdev=$(mapdevfs $path)
  1005.             if [ "$device" = "$testdev" ]; then
  1006.                 echo "$message" > $id/locked
  1007.             fi
  1008.         done
  1009.         close_dialog
  1010.     done
  1011. }
  1012.  
  1013. # Unlock a device or partition to allow further modifications
  1014. partman_unlock_unit() {
  1015.     local device dev testdev
  1016.     device="$1"
  1017.  
  1018.     for dev in $DEVICES/*; do
  1019.         [ -d "$dev" ] || continue
  1020.         cd $dev
  1021.  
  1022.         # First check if we should unlock a device
  1023.         if [ -e "device" ]; then
  1024.             testdev=$(mapdevfs $(cat device))
  1025.             if [ "$device" = "$testdev" ]; then
  1026.                 rm -f locked
  1027.                 return 0
  1028.             fi
  1029.         fi
  1030.  
  1031.         # Second check if we should unlock a partition
  1032.         open_dialog PARTITIONS
  1033.         while { read_line num id size type fs path name; [ "$id" ]; }; do
  1034.             testdev=$(mapdevfs $path)
  1035.             if [ "$device" = "$testdev" ]; then
  1036.                 rm -f $id/locked
  1037.             fi
  1038.         done
  1039.         close_dialog
  1040.     done
  1041. }
  1042.  
  1043. # List the changes that are about to be committed and let the user confirm first
  1044. confirm_changes () {
  1045.     local template dev x part partitions num id size type fs path name filesystem partdesc partitems items formatted_previously
  1046.     template="$1"
  1047.  
  1048.     # Compute the changes we are going to do
  1049.     partitems=''
  1050.     items=''
  1051.     formatted_previously=no
  1052.     for dev in $DEVICES/*; do
  1053.         [ -d "$dev" ] || continue
  1054.         cd $dev
  1055.  
  1056.         open_dialog IS_CHANGED
  1057.         read_line x
  1058.         close_dialog
  1059.         if [ "$x" = yes ]; then
  1060.             partitems="${partitems}   $(humandev $(cat device))
  1061. "
  1062.         fi
  1063.  
  1064.         partitions=
  1065.         open_dialog PARTITIONS
  1066.         while { read_line num id size type fs path name; [ "$id" ]; }; do
  1067.             [ "$fs" != free ] || continue
  1068.             partitions="$partitions $id,$num"
  1069.         done
  1070.         close_dialog
  1071.     
  1072.         for part in $partitions; do
  1073.             id=${part%,*}
  1074.             num=${part#*,}
  1075.             [ -f $id/method -a -f $id/format \
  1076.               -a -f $id/visual_filesystem ] || continue
  1077.             # if no filesystem (e.g. swap) should either be not
  1078.             # formatted or formatted before the method is specified
  1079.             [ -f $id/filesystem -o ! -f $id/formatted \
  1080.               -o $id/formatted -ot $id/method ] || continue
  1081.             # if it is already formatted filesystem it must be formatted 
  1082.             # before the method or filesystem is specified
  1083.             [ ! -f $id/filesystem -o ! -f $id/formatted \
  1084.               -o $id/formatted -ot $id/method \
  1085.               -o $id/formatted -ot $id/filesystem ] ||
  1086.             {
  1087.                 formatted_previously=yes
  1088.                 continue
  1089.             }
  1090.             filesystem=$(cat $id/visual_filesystem)
  1091.             # Special case d-m devices to use a different description
  1092.             if cat device | grep -q "/dev/mapper" ; then
  1093.                 partdesc="partman/text/confirm_unpartitioned_item"
  1094.             else
  1095.                 partdesc="partman/text/confirm_item"
  1096.                 db_subst $partdesc PARTITION "$num"
  1097.             fi
  1098.             db_subst $partdesc TYPE "$filesystem"
  1099.             db_subst $partdesc DEVICE $(humandev $(cat device))
  1100.             db_metaget $partdesc description
  1101.             
  1102.             items="${items}   ${RET}
  1103. "
  1104.         done
  1105.     done
  1106.  
  1107.     if [ "$items" ]; then
  1108.         db_metaget partman/text/confirm_item_header description
  1109.         items="$RET
  1110. $items"
  1111.     fi
  1112.     
  1113.     if [ "$partitems" ]; then
  1114.         db_metaget partman/text/confirm_partitem_header description
  1115.         partitems="$RET
  1116. $partitems"
  1117.     fi
  1118.  
  1119.     if [ "$partitems$items" ]; then
  1120.         if [ -z "$items" ]; then
  1121.             x="$partitems"
  1122.         elif [ -z "$partitems" ]; then
  1123.             x="$items"
  1124.         else
  1125.             x="$partitems
  1126. $items"
  1127.         fi
  1128.         maybe_escape "$x" db_subst $template/confirm ITEMS
  1129.         db_input critical $template/confirm
  1130.         db_go || true
  1131.         db_get $template/confirm
  1132.         if [ "$RET" = false ]; then
  1133.             db_reset $template/confirm
  1134.             return 1
  1135.         else
  1136.             db_reset $template/confirm
  1137.             return 0
  1138.         fi
  1139.     else
  1140.         if [ "$formatted_previously" = no ]; then
  1141.             db_input critical $template/confirm_nochanges
  1142.             db_go || true
  1143.             db_get $template/confirm_nochanges
  1144.             if [ "$RET" = false ]; then
  1145.                 db_reset $template/confirm_nochanges
  1146.                 return 1
  1147.             else
  1148.                 db_reset $template/confirm_nochanges
  1149.                 return 0
  1150.             fi
  1151.         else
  1152.             return 0
  1153.         fi
  1154.     fi
  1155. }
  1156.  
  1157. log '*******************************************************'
  1158.  
  1159. # Local Variables:
  1160. # coding: utf-8
  1161. # End:
  1162.